package com.jxin.mall.config.configuration;

import com.jxin.mall.config.consts.RedisTempTypeEnum;
import com.jxin.mall.config.plan.RedisSerializerPlan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * redis 模板实例配置类
 * @author 蔡佳新
 * @version 1.0
 * @since jdk 1.8
 */
@Configuration
public class RedisConfiguration {
    /**
     * 获取stringJsonRedisTemplate调用模板
     * @param  lettuceConnectionFactory redis连接工程
     * @return {@link RedisTemplate} redis调用模板
     * @author 蔡佳新
     */
    @Bean("stringJsonRedisTemplate")
    public RedisTemplate<String, Object> stringJsonRedisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
        return getRedisTemplate(lettuceConnectionFactory, RedisTempTypeEnum.STRING_JSON_TEMP.getType());
    }
    /**
     * 获取protobufRedisTemplate调用模板
     * @param  lettuceConnectionFactory redis连接工程
     * @return {@link RedisTemplate} redis调用模板
     * @author 蔡佳新
     */
    @Bean("protobufRedisTemplate")
    public RedisTemplate<String, Object> ProtobufRedisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
        return getRedisTemplate(lettuceConnectionFactory, RedisTempTypeEnum.PROTOBUF_TEMP.getType());
    }
    /**
     * 获取multiProtobufRedisTemplate调用模板
     * @param  lettuceConnectionFactory redis连接工程
     * @return {@link RedisTemplate} redis调用模板
     * @author 蔡佳新
     */
    @Bean("multiProtobufRedisTemplate")
    public RedisTemplate<String, Object> MultiProtobufRedisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
        return getRedisTemplate(lettuceConnectionFactory, RedisTempTypeEnum.MULTI_PROTOBUF_TEMP.getType());
    }
    /**
     * 获取StringJsonRedisTemplate调用模板
     * @param  redisConnectionFactory redis连接工厂
     * @param  type 模板类型
     * @return {@link RedisTemplate} redis调用模板
     * @author 蔡佳新
     */
    private RedisTemplate<String, Object> getRedisTemplate(RedisConnectionFactory redisConnectionFactory, Integer type) {
        final RedisTemplate<String, Object> result = new RedisTemplate<>();
        if(RedisTempTypeEnum.MULTI_PROTOBUF_TEMP.getType().equals(type)){
            // 开启Redis 事务权限
            result.setEnableTransactionSupport(true);
        }
        result.setKeySerializer(new StringRedisSerializer());
        result.setHashKeySerializer(new StringRedisSerializer());
        final RedisSerializer<Object> objectRedisSerializer = RedisSerializerPlan.valueSerializer(type);
        result.setValueSerializer(objectRedisSerializer);
        result.setHashValueSerializer(objectRedisSerializer);
        result.setConnectionFactory(redisConnectionFactory);
        return result;
    }


}
